//
//  LEETabBar.swift
//  SinaWeibo
//
//  Created by 李江波 on 2016/11/12.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit

class LEETabBar: UITabBar {

    var btnClick : (() -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(btn)
        btn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func btnClick(btn : UIButton) {
        
        btnClick?()
    }
    

    //修改内部视图的位置 应该重写 layoutSubviews
    //布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
//        print(subviews)
        
        let itemW = UIScreen.main.bounds.width / 5
        let itemH = self.bounds.height
        var index = 0
        
        for item in subviews {
            
            if item.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                item.frame = CGRect(x: CGFloat(index) * itemW, y: 0, width: itemW, height: itemH)
                
                index += (index == 1 ? 2 : 1)
            }
        }
        
        //设置中间按钮frame
        btn.bounds.size = CGSize(width: itemW, height: itemH)
        btn.center = CGPoint(x: self.center.x, y: self.bounds.height * 0.5)
        
    }
    
    
    lazy var btn : UIButton = {
    
        let btn = UIButton()
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(#imageLiteral(resourceName: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "tabbar_compose_button_highlighted"), for: .highlighted)
        
        return btn
    }()
    
    
}
